rm(list=ls())
library(tidyverse)
library(plyr)
library(scales)
library(dplyr)
library(rmeta)
library(xtable)
library(stargazer)
library(data.table)

source('Habit_source.R', echo=TRUE)
load("rdd_fl09.Rdata")
fl09=data
load("rdd_fl.Rdata")

fl09=as.data.table(fl09)

data=as.data.table(data)


fl09$dob = as.character(fl09$dob)
data$dob=as.character(data$dob)

out = merge(data, fl09[,c('dob','vote2008_hispanic', 'vote2008_white', 'vote2008_black')],
            all.x=T, by = 'dob')



data = as.data.frame(out)
rm(out,fl09)


data$forcing = as.integer(as.Date("1990-11-04"))- as.integer(as.Date(data$dob))
data$Cutoff08 = ifelse(data$forcing >= 0, 1, 0)



################# DV: Vote 2016 ########################

ivmod1_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_white", 
                                            downstream_voted = "vote.gen2016_white", downstream_lag = "lag2016_white", bandwidth = 365, state.abbrev=NULL)


ivmod1_black = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_black", 
                                            downstream_voted = "vote.gen2016_black", downstream_lag = "lag2016_black", bandwidth = 365, state.abbrev=NULL)
ivmod1_hispanic = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_hispanic", 
                                               downstream_voted = "vote.gen2016_hispanic", downstream_lag = "lag2016_hispanic", bandwidth = 365, state.abbrev=NULL)


##################### DV: Vote 2014 ##########################


ivmod2_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_white", 
                                            downstream_voted = "vote.gen2014_white", downstream_lag = "lag2014_white", bandwidth = 365, state.abbrev=NULL)
ivmod2_black = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_black", 
                                            downstream_voted = "vote.gen2014_black", downstream_lag = "lag2014_black", bandwidth = 365, state.abbrev=NULL)
ivmod2_hispanic = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_hispanic", 
                                               downstream_voted = "vote.gen2014_hispanic", downstream_lag = "lag2014_hispanic", bandwidth = 365, state.abbrev=NULL)


##################### DV: Vote 2012 ##########################

ivmod3_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_white", 
                                            downstream_voted = "vote.gen2012_white", downstream_lag = "lag2012_white", bandwidth = 365, state.abbrev=NULL)

ivmod3_black = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_black", 
                                            downstream_voted = "vote.gen2012_black", downstream_lag = "lag2012_black", bandwidth = 365, state.abbrev=NULL)
ivmod3_hispanic = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_hispanic", 
                                               downstream_voted = "vote.gen2012_hispanic", downstream_lag = "lag2012_hispanic", bandwidth = 365, state.abbrev=NULL)



##################### DV: Vote 2010 ##########################


ivmod4_white = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_white", 
                                            downstream_voted = "vote.gen2010_white", downstream_lag = "lag2010_white", bandwidth = 365, state.abbrev=NULL)

ivmod4_black = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_black", 
                                            downstream_voted = "vote.gen2010_black", downstream_lag = "lag2010_black", bandwidth = 365, state.abbrev=NULL)
ivmod4_hispanic = state.cace.estimator.primary(data = data, upstream_days = "forcing", upstream_treat = "Cutoff08", upstream_voted = "vote2008_hispanic", 
                                               downstream_voted = "vote.gen2010_hispanic", downstream_lag = "lag2010_hispanic", bandwidth = 365, state.abbrev=NULL)










rm(data)
save.image(file = "results/results_fl09.Rdata")



#########
rm(list=ls())


states = c('fl', 'fl09')

results = as.data.frame(matrix(nrow = 0, ncol = 7))
colnames(results) = c("coef", "se", "race", "dv", "p value", "endog", "state")



for(state in states){
  f = paste0('results/results_',state,'.Rdata')
  load(f)
  holder = as.data.frame(matrix(nrow = 12, ncol = 7))
  colnames(holder) = c("coef", "se", "race", "dv", "p value", "endog", "state")
  
  holder$coef = c(ivmod1_white$cace, ivmod1_black$cace, ivmod1_hispanic$cace,
                  ivmod2_white$cace,  ivmod2_black$cace, ivmod2_hispanic$cace,
                  ivmod3_white$cace,  ivmod3_black$cace, ivmod3_hispanic$cace,
                  ivmod4_white$cace,  ivmod4_black$cace, ivmod4_hispanic$cace
                  
                  
                  
                  
                  
                  
                  
                  
                  
  )
  
  holder$se = c(ivmod1_white$se,  ivmod1_black$se, ivmod1_hispanic$se,
                ivmod2_white$se,  ivmod2_black$se, ivmod2_hispanic$se,
                ivmod3_white$se,  ivmod3_black$se, ivmod3_hispanic$se,
                ivmod4_white$se,  ivmod4_black$se, ivmod4_hispanic$se
                
                
                
                
                
                
  )
  
  holder$`p value` = c(ivmod1_white$summary$coefficients[2,4],  ivmod1_black$summary$coefficients[2,4], ivmod1_hispanic$summary$coefficients[2,4],
                       ivmod2_white$summary$coefficients[2,4],  ivmod2_black$summary$coefficients[2,4], ivmod2_hispanic$summary$coefficients[2,4],
                       ivmod3_white$summary$coefficients[2,4],  ivmod3_black$summary$coefficients[2,4], ivmod3_hispanic$summary$coefficients[2,4],
                       ivmod4_white$summary$coefficients[2,4],  ivmod4_black$summary$coefficients[2,4], ivmod4_hispanic$summary$coefficients[2,4]
                       
                       
                       
                       
                       
  )
  ############itt
  
  ##############
  holder$race = c(rep(c("white", "black", "hispanic"), 4))
  
  holder$dv = c(c(rep("Vote 2016", 3),
                  rep("Vote 2014", 3),
                  rep("Vote 2012", 3),
                  rep("Vote 2010", 3)))
  
  
  holder$endog = c(rep("Vote 2008", 12))
  holder$state = state
  
  
  results = rbind(results, holder)
  
  print(f); print(nrow(results))
}







plt = results%>%filter(endog == "Vote 2008" & race %in% c('white', 'hispanic', 'black') & !is.na(race) )%>%
  mutate(race = factor(Hmisc::capitalize(race), levels = c('Black', 'White', 'Hispanic')),
         state = ifelse(state=='fl09', 'Using 2009 Voterfile', 'Main Results'))

ggsave(filename = "figures/FigS9.pdf", width=9,height=5,
ggplot(data = plt[plt$dv %in% c("Vote 2010", "Vote 2012", "Vote 2014", "Vote 2016"),], aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape = 21, size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2008 Voting on Future Voting') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +

  scale_color_manual(values = c("black", "black", "black"))+
  scale_fill_manual(values = c("black", "white", "grey"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  scale_x_discrete(labels = c("2010", "2012", "2014", "2016"))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  facet_wrap(state~., nrow = 3)
)





